package ar.edu.unq.agenda.ui;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.bindings.NotNullObservable;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.Selector;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.widgets.tables.Table;
import org.uqbar.arena.windows.Dialog;
import org.uqbar.arena.windows.SimpleWindow;
import org.uqbar.arena.windows.WindowOwner;
import org.uqbar.commons.model.ObservableObject;
import org.uqbar.commons.model.Search;
import org.uqbar.commons.model.SearchByExample;

import ar.unq.edu.agenda.domain.City;
import ar.unq.edu.agenda.domain.Contact;
import ar.unq.edu.agenda.home.Agenda;

public class ContactSearchWindow<E extends ObservableObject, T extends Search<E>> extends SimpleWindow<T> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	

	public ContactSearchWindow(WindowOwner owner, T model) {
		super(owner, model);
	}

	@Override
	protected void createMainTemplate(Panel mainPanel) {
		this.createFormPanel(mainPanel);
		this.createActionsPanel(mainPanel);
		this.createResultsGrid(mainPanel);
		this.createGridActions(mainPanel);
	}

	@Override
	protected void createActionsPanel(Panel formBuilder) {
		super.createActionsPanel(formBuilder);
	}

	// ***********************************************************
	// ** Grid
	// ***********************************************************

	protected void createResultsGrid(Panel mainPanel) {
		Table<Contact> table = new Table<Contact>(mainPanel, Contact.class);

		table.bindContentsToProperty(Search.RESULTS);
		table.bindSelection(Search.SELECTED);

		this.describeResultsGrid(table);
	}

	protected void describeResultsGrid(Table<Contact> builder)
	{
		//Declaro la columna para el nombre
		Column<Contact> nombreColumn = new Column<Contact>(builder);
		nombreColumn.setTitle("Nombre");
		nombreColumn.setPreferredSize(100);
		nombreColumn.bindContentsToProperty(Contact.NAME);
		
		//Declaro la columna para el apellido
		Column<Contact> lastName = new Column<Contact>(builder);
		lastName.setTitle("Apellido");
		lastName.setPreferredSize(100);
		lastName.bindContentsToProperty(Contact.LASTNAME);	
		
		//Declaro la columna para el phone
		Column<Contact> phone = new Column<Contact>(builder);
		phone.setTitle("Telefono");
		phone.setPreferredSize(100);
		phone.bindContentsToProperty(Contact.PHONE);
		
		//Declaro la columna para la fecha de nacimiento
		Column<Contact> birthDate = new Column<Contact>(builder);
		birthDate.setTitle("Fecha de Nacimiento");
		birthDate.setPreferredSize(100);
		birthDate.bindContentsToProperty(Contact.BIRTHDATE);			
	
		//Declaro la columna para la ciudad
		Column<Contact> city = new Column<Contact>(builder);
		city.setTitle("Ciudad");
		city.setPreferredSize(100);
		city.bindContentsToProperty(City.NAME);	
		
	}

	// ***********************************************************
	// ** Actions
	// ***********************************************************

	@Override
	protected void addActions(Panel actionsPanel) {
		Button buscar = new Button(actionsPanel);
		buscar.setCaption("Buscar");
		buscar.onClick(new MessageSend(this.getModel(), Search.SEARCH));
		buscar.setAsDefault();
		
		Button nuevoSocio = new Button(actionsPanel);
		nuevoSocio.setCaption("Nuevo Socio");
		nuevoSocio.onClick(new MessageSend(this, "createContact"));	
	}
	
	public void createContact()
	{
		SimpleWindow<?> createContact = new ContactWindowCreate(this);
		createContact.open();
		
	}


	
	protected void createGridActions(Panel mainPanel) {
		Panel actionsPanel = new Panel(mainPanel);
		actionsPanel.setHorizontalLayout();

		NotNullObservable elementSelected = new NotNullObservable(Search.SELECTED);

		Button edit = new Button(actionsPanel);
		edit.setCaption("Edit");
		edit.bindEnabled(elementSelected);
		edit.onClick(new MessageSend(this, "startEdition"));

		Button remove = new Button(actionsPanel);
		remove.setCaption("Remove");
		remove.bindEnabled(new NotNullObservable(Search.SELECTED));
		remove.onClick(new MessageSend(this.getModel(), "removeSelected"));
	}

	// ********************************************************
	// ** Actions
	// ********************************************************

	public void startEdition() {
		SimpleWindow<?> editor = this.createEditor(this.getModel().getSelected());
		editor.open();
	}

	protected  SimpleWindow<?> createEditor(E selected){
		return new ContactEditWindow(this, (Contact)selected);
	}

	/**
	 * Define el formulario del buscador, que contendrá un campo de texto que permita 
	 * la búsqueda por los siguientes atributos
	 * -Nombre
	 * -Apellido
	 * -Email
	 * -Ciudad
	 *  @author nicolas
	 */
	@Override
	protected void createFormPanel(Panel mainPanel) 
	{
		Panel searchFormPanel = new Panel(mainPanel);
		searchFormPanel.bindContents(SearchByExample.EXAMPLE);
		searchFormPanel.setLayout(new ColumnLayout(2));
		
		//Defino Label y TextBox para el nombre
		Label labelName = new Label(searchFormPanel);
		labelName.setText("Nombre");		
		TextBox name = new TextBox(searchFormPanel);
		name.bindValueToProperty(Contact.NAME);
		
		//Defino Label y TextBox para el apellido
		Label labelLastName = new Label(searchFormPanel);
		labelLastName.setText("Apelido");		
		TextBox lastName = new TextBox(searchFormPanel);
		lastName.bindValueToProperty(Contact.LASTNAME);
		
		//Defino Label y Selector para la ciudad
		Label labelCity = new Label(searchFormPanel);
		labelCity.setText("Ciudad");
		Selector selectorCities =  new Selector(searchFormPanel);
		selectorCities.setContents(Agenda.getHome(City.class).allInstances(),City.NAME).bindValueToProperty(Contact.CITY);
		selectorCities.bindValueToProperty(Contact.CITY);
		
	}

}
